from urllib import request
import re

pageindex = int(input('请输入想查询的页码：'))
base_url = 'https://bj.58.com/chuzu/pn%d/?PGTID=0d3090a7-000a-ca3c-b34d-fb348e9bed61&ClickID=2' % (pageindex)
print(base_url)
# 执行请求信息
req = request.Request(base_url)
try:
    # 获取响应内容
    res = request.urlopen(req)
    html = res.read().decode('utf-8')
    # 正则表达式匹配图片，标题，户型，价格
    pat = 'lazy_src="(.*?)".*?<span class="picNum">.*?</span>.*?<h2>.*?<a href=".*?".*?class="strongbox".*?>(.*?)</a>.*?</h2>.*?<p class="room strongbox">(.*?)</p>.*?<b class="strongbox">(.*?)</b>(.*?)</div>'
    dlist = re.findall(pat,html,re.S)
    for d in dlist:
        print('图片地址：' + d[0] + '\t'+ '标题：' + str(re.sub('\s','',d[1])) + '\t' + '户型：' + str(re.sub('\s|&nbsp;','',d[2])) + '\t'+ '价格：' + d[3]+d[4])
except Exception as e:
    if hasattr(e,'reason'):
        print(e.reason)

    if hasattr(e,'code'):
        print(e.code)
print('58同城第{}页租房信息爬取完成'.format(pageindex))

